package com.jinboe.comm.util;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.ConvertUtils;

/**
* 重写BeanUtils.copyProperties
*
* @author jinbo
*/
public class BeanUtils extends org.apache.commons.beanutils.BeanUtils {
   static {
       ConvertUtils.register(new DateConvert(), java.util.Date.class);
       ConvertUtils.register(new DateConvert(), java.lang.String.class);
   }
   public static void copyProperties(Object dest, Object orig) {
       try {
    	   org.apache.commons.beanutils.BeanUtils.copyProperties(dest, orig);
       } catch (Exception ex) {
           ex.printStackTrace();
       }
   }
   /**
    * 取得对象属性列表
    * @param bean
    * @return
    * @throws Exception
    */
   public static List<Map> getBeanProperties(Object bean) throws Exception {
	   List <Map> propertiesList=new ArrayList();
       //得到类对象
       Class userCla = (Class) bean.getClass();
       /*
        * 得到类中的所有属性集合
        */
       Field[] fs = userCla.getDeclaredFields();
       for(int i = 0 ; i < fs.length; i++){
           Field f = fs[i];
           f.setAccessible(true); //设置些属性是可以访问的 
           Map propMap=new HashMap();
           propMap.put(f.getName(),f.getType());
           propertiesList.add(propMap);
       }
       return propertiesList;
   }
   public static void setBeanProperties(Object obj, String att, Object value, Class<?>type){
	   try {  
           Method met = obj.getClass().getMethod("set" + initStr(att), type);  
           met.invoke(obj, value);  
       }catch (Exception e){  
           e.printStackTrace();  
       }  
	   
   }
   public static String initStr(String old){   // 将单词的首字母大写  
       String str = old.substring(0,1).toUpperCase() + old.substring(1) ;  
       return str ;  
   }  
}
